/**
 * Created by Administrator on 2017/1/13.
 */

var LogicSubModules = require('../../../logic/sub');
var roomManager = require('../../../logic/game/roomManager');
var gameUtil = require("../../../util/gameUtil");

module.exports = function (app) {
    return new RoomRemote(app);
};

var RoomRemote = function (app) {
    this.app = app;
};

RoomRemote.prototype.create = function (uid, msg, cb) {
    logger.info("roomRemote.create uid=%d msg=%j", uid, msg);

    var rid = +msg.rid;
    if (roomManager.getRoom(rid)) {
        cb(Code.ROOM_EXIST);
        return;
    }

    var SubLogic = LogicSubModules[msg.gameType];

    var room = new SubLogic.Room(rid, uid, msg);
    if (!room.checkInit()) {
        cb(Code.ROOM_CHECK_FAILED);
        return;
    }

    roomManager.addRoom(room);
    cb(Code.SUCCESS);
}

RoomRemote.prototype.enter = function (uid, msg, cb) {
    var rid = +msg.rid;
    var sid = msg.sid;
    var isMember = msg.isMember;

    var room = roomManager.getRoom(rid);
    if (!room || room.isDestroyed()) {
        cb(Code.ROOM_INVALID);
        return;
    }
    if (room.isAgency()) {
        if (!isMember) {
            cb(Code.NOT_GROUP_MEMBER);
            return;
        }
    }

    var player = room.getPlayerByUid(uid);
    if (player) {
        player.initUser(msg);
        room.handlePlayerReconnect(sid, player, cb);
        return;
    }

    var roomConfig = room.getRoomConfig();
    if (room.isCardRoom() && room.getCurRound() > 0
        && !roomConfig.enableJoinAfterStart) {
        cb(Code.ROOM_PLAYING);
        return;
    }

    var SubLogic = LogicSubModules[room.getGameType()];
    var playerIndex = room.allocateIndex(uid);
    if (!playerIndex) {
        cb(Code.ROOM_FULL);
        return;
    }

    if (room.isCardRoom()) {
        var userResource = msg.user;
        var needResource = gameUtil.calcCreateRoomNeedResource(SubLogic, room.getInitArgs());

        switch (room.getCostMode()) {
            case Def.RoomCostMode.AA:
                var aaNeedResource = ExObject.mulNew(needResource, 1 / room.getMaxPlayer(), Math.ceil);
                if (!ExObject.numberGTE(userResource, aaNeedResource)) {
                    cb(Code.LACK_OF_RESOURCE);
                    return;
                }
                break;
            case Def.RoomCostMode.WINNER:
                if (!ExObject.numberGTE(msg.user, needResource)) {
                    cb(Code.LACK_OF_RESOURCE);
                    return;
                }
                break;
            case Def.RoomCostMode.ALL:
                if (!ExObject.numberGTE(msg.user, needResource)) {
                    cb(Code.LACK_OF_RESOURCE);
                    return;
                }
                break;
            default:
                break;
        }
    }

    player = new SubLogic.GamePlayer(uid, msg);
    //每个玩家加入判断支付方式
    room.addPlayer(playerIndex, player);
    room.handlePlayerEnter(sid, player, cb);
}
RoomRemote.prototype.query = function (uid, msg, cb) {
    logger.info("roomRemote.query uid=%d msg=%j", uid, msg);
    var rid = msg.rid;
    console.log("ridssss")
    console.log(rid);

    var room = roomManager.getRoom(rid)
    if (!room || room.isDestroyed()) {
        cb(Code.ROOM_INVALID);
        return;
    } else {
        cb(null, {
            "id": room.rid(),
            "createTime": room.getCreateTime(),
            "options": room.getOptions(),
            "subType": room.getSubType(),
            "round": room.getRound(),
            "curRound": room.getCurRound(),
            "costMode": room.getCostMode(),
            "playerNum": room.getPlayerCount(),
            "players": room.getInfo().players
        });
    }
}
RoomRemote.prototype.compelDestroy = function (uid, msg, cb) {
    logger.info("roomRemote.getRoom uid=%d msg=%j", uid, msg);
    var rid = msg.rid;
    console.log("ridssss");
    console.log(rid);

    var room = roomManager.getRoom(rid);
    if (!room || room.isDestroyed()) {
        cb(Code.ROOM_INVALID);
        return;
    } else {
        if (uid == room.getCreator()) {
            room.destroy();
            cb(null, {"destroy": true});
        } else {
            cb(null, {"destroy": false});
        }
    }
}